# Clear
rm(list = ls())

# Working Directory
getwd()
list.files()
setwd("/Users/claywebb/Dropbox/KU/Research/Sanctions/Power Politics or Public Pandering/IIRR")

#######################
###                 ###
### Create Data Set ###
###                 ###
#######################


## Load TIES Data - Downloaded from https://www.unc.edu/~bapat/TIES.htm
sdat <- read.csv("TIES.csv")

## Create Date Variable

### Sort Dataset by Year, Month, and Day
sortsdat <- sdat[order(sdat$startyear,sdat$startmonth,sdat$startday),]
head(sortsdat)
tail(sortsdat)

### Function to Generate Monthly Time Series
my_monthly <- function(start,end){
  
  month <- list()
  year <- list()
  
  for (i in start:end){
    
    year[[i]] <- rep(i,12)
    
    
  }
  
  for (i in start:end){
    
    month[[i]] <- seq(1,12)
    
  }
  
  yearv <- matrix(unlist(year),ncol = 1)
  yearv
  
  monthv <- matrix(unlist(month),ncol = 1)
  monthv
  
  date_out <- cbind(yearv,monthv)
  colnames(date_out) <- c("year","month")
  date_out
}

### Create Dates
start <- 1945
end <- 2005
frequency <- "month"  
my_dates <- data.frame(my_monthly(1945,2005))

## Aggregate Monthly Counts of U.S. Sanctions

### Give each unique sanction a value of one
sdat$sanctions <- 1
head(sdat)
tail(sdat)

### Restrict to U.S. Sanctions
us_sanc <- subset(subset(sdat,primarysender==2), imposition==1 | finaloutcome > 5 | imposition==1 & finaloutcome > 5)

### Aggregate U.S. sanctions into a monthly time series 

#### Use table() to Aggregate U.S. Sanctions into monthly counts and as.data.frame() to save these data as a data frame
us_sanc_df <- as.data.frame(table(us_sanc$startmonth,
                                  us_sanc$startyear,
                                  us_sanc$sanctions))

#### Assign names for variables
colnames(us_sanc_df) <-c("month","year","one","sanc")

#### Drop column of ones
us_sanc_df$one <- NULL

#### Merge U.S. sanctions with created date variable
nsd <- merge(my_dates,us_sanc_df, by=c("month","year"),all = TRUE)

#### Assign 0 to months where there are no sancitons
nsd$sanc[is.na(nsd$sanc)] <- 0

#### Save as data frame
nsd <- as.data.frame(nsd[order(nsd$year,nsd$month),])

## Generate High Politics, Low Politics, and Issue Counts

### High Politics
us_sanc_hp <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1== 1 | issue1 == 2 | issue1 == 3 | issue1 == 4 | issue1 == 5 | issue1 == 6 | issue1 == 7 | issue1 == 8 | issue1 == 9 | issue1 == 10 | issue1 == 11 | issue1 == 12)
us_sanc_hp_df <- as.data.frame(table(us_sanc_hp$startmonth, us_sanc_hp$startyear, us_sanc_hp$sanctions))
colnames(us_sanc_hp_df) <-c("month","year","one","hp")
us_sanc_hp_df$one <- NULL
nsd <- merge(nsd,us_sanc_hp_df, by=c("month","year"),all = TRUE)
nsd$hp[is.na(nsd$hp)] <- 0

### Low Politics
us_sanc_lp <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1== 13 | issue1 == 14 | issue1 == 15)
us_sanc_lp_df <- as.data.frame(table(us_sanc_lp$startmonth, us_sanc_lp$startyear, us_sanc_lp$sanctions))
colnames(us_sanc_lp_df) <-c("month","year","one","lp")
us_sanc_lp_df$one <- NULL
nsd <- merge(nsd,us_sanc_lp_df, by=c("month","year"),all = TRUE)
nsd$lp[is.na(nsd$lp)] <- 0

## Specific Issues

### Contain Political Influence
us_sanc_pol_inf <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==1)
us_sanc_pol_inf_df <- as.data.frame(table(us_sanc_pol_inf$startmonth, us_sanc_pol_inf$startyear, us_sanc_pol_inf$sanctions))
colnames(us_sanc_pol_inf_df) <-c("month","year","one","pol_inf")
us_sanc_pol_inf_df$one <- NULL
nsd <- merge(nsd,us_sanc_pol_inf_df, by=c("month","year"),all = TRUE)
nsd$pol_inf[is.na(nsd$pol_inf)] <- 0

### Contain Military Behavior
us_sanc_con_mil <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==2)
us_sanc_con_mil_df <- as.data.frame(table(us_sanc_con_mil$startmonth, us_sanc_con_mil$startyear, us_sanc_con_mil$sanctions))
colnames(us_sanc_con_mil_df) <-c("month","year","one","con_mil")
us_sanc_con_mil_df$one <- NULL
nsd <- merge(nsd,us_sanc_con_mil_df, by=c("month","year"),all = TRUE)
nsd$con_mil[is.na(nsd$con_mil)] <- 0

### Destabilize Regime
us_sanc_destab_reg <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==3)
us_sanc_destab_reg_df <- as.data.frame(table(us_sanc_destab_reg$startmonth, us_sanc_destab_reg$startyear, us_sanc_destab_reg$sanctions))
colnames(us_sanc_destab_reg_df) <-c("month","year","one","destab_reg")
us_sanc_destab_reg_df$one <- NULL
nsd <- merge(nsd,us_sanc_destab_reg_df, by=c("month","year"),all = TRUE)
nsd$destab_reg[is.na(nsd$destab_reg)] <- 0

### Release Citizens, Property, or Material
us_sanc_rel_cpm <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==4)
us_sanc_rel_cpm_df <- as.data.frame(table(us_sanc_rel_cpm$startmonth, us_sanc_rel_cpm$startyear, us_sanc_rel_cpm$sanctions))
colnames(us_sanc_rel_cpm_df) <-c("month","year","one","rel_cpm")
us_sanc_rel_cpm_df$one <- NULL
nsd <- merge(nsd,us_sanc_rel_cpm_df, by=c("month","year"),all = TRUE)
nsd$rel_cpm[is.na(nsd$rel_cpm)] <- 0

### Solve Territorial Dispute
us_sanc_territory <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==5)
us_sanc_territory_df <- as.data.frame(table(us_sanc_territory$startmonth, us_sanc_territory$startyear, us_sanc_territory$sanctions))
colnames(us_sanc_territory_df) <-c("month","year","one","territory")
us_sanc_territory_df$one <- NULL
nsd <- merge(nsd,us_sanc_territory_df, by=c("month","year"),all = TRUE)
nsd$territory[is.na(nsd$territory)] <- 0

### Deny Strategic Materials
us_sanc_strat_mat <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==6)
us_sanc_strat_mat_df <- as.data.frame(table(us_sanc_strat_mat$startmonth, us_sanc_strat_mat$startyear, us_sanc_strat_mat$sanctions))
colnames(us_sanc_strat_mat_df) <-c("month","year","one","strat_mat")
us_sanc_strat_mat_df$one <- NULL
nsd <- merge(nsd,us_sanc_strat_mat_df, by=c("month","year"),all = TRUE)
nsd$strat_mat[is.na(nsd$strat_mat)] <- 0

### Retaliate Against Alliance or Alignment Choice
us_sanc_ally <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==7)
us_sanc_ally_df <- as.data.frame(table(us_sanc_ally$startmonth, us_sanc_ally$startyear, us_sanc_ally$sanctions))
colnames(us_sanc_ally_df) <-c("month","year","one","ally")
us_sanc_ally_df$one <- NULL
nsd <- merge(nsd,us_sanc_ally_df, by=c("month","year"),all = TRUE)
nsd$ally[is.na(nsd$ally)] <- 0

### Improve Human Rights
us_sanc_hr <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==8)
us_sanc_hr_df <- as.data.frame(table(us_sanc_hr$startmonth, us_sanc_hr$startyear, us_sanc_hr$sanctions))
colnames(us_sanc_hr_df) <-c("month","year","one","hr")
us_sanc_hr_df$one <- NULL
nsd <- merge(nsd,us_sanc_hr_df, by=c("month","year"),all = TRUE)
nsd$hr[is.na(nsd$hr)] <- 0

### End Weapons/Materials Proliferation
us_sanc_prolif <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==9)
us_sanc_prolif_df <- as.data.frame(table(us_sanc_prolif$startmonth, us_sanc_prolif$startyear, us_sanc_prolif$sanctions))
colnames(us_sanc_prolif_df) <-c("month","year","one","prolif")
us_sanc_prolif_df$one <- NULL
nsd <- merge(nsd,us_sanc_prolif_df, by=c("month","year"),all = TRUE)
head(nsd)
nsd$prolif[is.na(nsd$prolif)] <- 0

### Terminate Support for Non-State Actors
us_sanc_nonstate <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==10)
us_sanc_nonstate_df <- as.data.frame(table(us_sanc_nonstate$startmonth, us_sanc_nonstate$startyear, us_sanc_nonstate$sanctions))
colnames(us_sanc_nonstate_df) <-c("month","year","one","nonstate")
us_sanc_nonstate_df$one <- NULL
nsd <- merge(nsd,us_sanc_nonstate_df, by=c("month","year"),all = TRUE)
nsd$nonstate[is.na(nsd$nonstate)] <- 0

### Deter or Punish Drug Trafficking
us_sanc_drug <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==11)
us_sanc_drug_df <- as.data.frame(table(us_sanc_drug$startmonth, us_sanc_drug$startyear, us_sanc_drug$sanctions))
colnames(us_sanc_drug_df) <-c("month","year","one","drug")
us_sanc_drug_df$one <- NULL
nsd <- merge(nsd,us_sanc_drug_df, by=c("month","year"),all = TRUE)
nsd$drug[is.na(nsd$drug)] <- 0

### Environmental Policies
us_sanc_environment <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==12)
us_sanc_environment_df <- as.data.frame(table(us_sanc_environment$startmonth, us_sanc_environment$startyear, us_sanc_environment$sanctions))
colnames(us_sanc_environment_df) <-c("month","year","one","environment")
us_sanc_environment_df$one <- NULL
nsd <- merge(nsd,us_sanc_environment_df, by=c("month","year"),all = TRUE)
nsd$environment[is.na(nsd$environment)] <- 0

### Trade Practices
us_sanc_trade <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==13)
us_sanc_trade_df <- as.data.frame(table(us_sanc_trade$startmonth, us_sanc_trade$startyear, us_sanc_trade$sanctions))
colnames(us_sanc_trade_df) <-c("month","year","one","trade")
us_sanc_trade_df$one <- NULL
nsd <- merge(nsd,us_sanc_trade_df, by=c("month","year"),all = TRUE)
nsd$trade[is.na(nsd$trade)] <- 0

### Economic Reform
us_sanc_econ <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==14)
us_sanc_econ_df <- as.data.frame(table(us_sanc_econ$startmonth, us_sanc_econ$startyear, us_sanc_econ$sanctions))
colnames(us_sanc_econ_df) <-c("month","year","one","econ")
us_sanc_econ_df$one <- NULL
nsd <- merge(nsd,us_sanc_econ_df, by=c("month","year"),all = TRUE)
nsd$econ[is.na(nsd$econ)] <- 0

### Other
us_sanc_other <- subset(subset(subset(sdat,primarysender==2), imposition==1), issue1==15)
us_sanc_other_df <- as.data.frame(table(us_sanc_other$startmonth, us_sanc_other$startyear, us_sanc_other$sanctions))
colnames(us_sanc_other_df) <-c("month","year","one","other")
us_sanc_other_df$one <- NULL
nsd <- merge(nsd,us_sanc_other_df, by=c("month","year"),all = TRUE)
nsd$other[is.na(nsd$other)] <- 0

## Target Countrires

### Canada (20)
us_sanc_canada <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==20)
us_sanc_canada_df <- as.data.frame(table(us_sanc_canada$startmonth, us_sanc_canada$startyear, us_sanc_canada$sanctions))
colnames(us_sanc_canada_df) <-c("month","year","one","canada")
us_sanc_canada_df$one <- NULL
nsd <- merge(nsd,us_sanc_canada_df, by=c("month","year"),all = TRUE)
nsd$canada[is.na(nsd$canada)] <- 0

### Cuba (40)
us_sanc_cuba <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==40)
us_sanc_cuba_df <- as.data.frame(table(us_sanc_cuba$startmonth, us_sanc_cuba$startyear, us_sanc_cuba$sanctions))
colnames(us_sanc_cuba_df) <-c("month","year","one","cuba")
us_sanc_cuba_df$one <- NULL
nsd <- merge(nsd,us_sanc_cuba_df, by=c("month","year"),all = TRUE)
nsd$cuba[is.na(nsd$cuba)] <- 0

### Haiti (41)
us_sanc_haiti <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==41)
us_sanc_haiti_df <- as.data.frame(table(us_sanc_haiti$startmonth, us_sanc_haiti$startyear, us_sanc_haiti$sanctions))
colnames(us_sanc_haiti_df) <-c("month","year","one","haiti")
us_sanc_haiti_df$one <- NULL
nsd <- merge(nsd,us_sanc_haiti_df, by=c("month","year"),all = TRUE)
nsd$haiti[is.na(nsd$haiti)] <- 0

### Dominican Republic (42)
us_sanc_domrep <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==42)
us_sanc_domrep_df <- as.data.frame(table(us_sanc_domrep$startmonth, us_sanc_domrep$startyear, us_sanc_domrep$sanctions))
colnames(us_sanc_domrep_df) <-c("month","year","one","domrep")
us_sanc_domrep_df$one <- NULL
nsd <- merge(nsd,us_sanc_domrep_df, by=c("month","year"),all = TRUE)
nsd$domrep[is.na(nsd$domrep)] <- 0

### Trinidad and Tobago (52)
us_sanc_trinto <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==52)
us_sanc_trinto_df <- as.data.frame(table(us_sanc_trinto$startmonth, us_sanc_trinto$startyear, us_sanc_trinto$sanctions))
colnames(us_sanc_trinto_df) <-c("month","year","one","trinto")
us_sanc_trinto_df$one <- NULL
nsd <- merge(nsd,us_sanc_trinto_df, by=c("month","year"),all = TRUE)
nsd$trinto[is.na(nsd$trinto)] <- 0

### Barbados (53)
us_sanc_barbados <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==53)
us_sanc_barbados_df <- as.data.frame(table(us_sanc_barbados$startmonth, us_sanc_barbados$startyear, us_sanc_barbados$sanctions))
colnames(us_sanc_barbados_df) <-c("month","year","one","barbados")
us_sanc_barbados_df$one <- NULL
nsd <- merge(nsd,us_sanc_barbados_df, by=c("month","year"),all = TRUE)
nsd$barbados[is.na(nsd$barbados)] <- 0
head(nsd)

### Dominica (54)
us_sanc_dominica <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==54)
us_sanc_dominica_df <- as.data.frame(table(us_sanc_dominica$startmonth, us_sanc_dominica$startyear, us_sanc_dominica$sanctions))
colnames(us_sanc_dominica_df) <-c("month","year","one","dominica")
us_sanc_dominica_df$one <- NULL
nsd <- merge(nsd,us_sanc_dominica_df, by=c("month","year"),all = TRUE)
nsd$dominica[is.na(nsd$dominica)] <- 0

### St Vincent and Grenadines (57)
us_sanc_svg <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==57)
us_sanc_svg_df <- as.data.frame(table(us_sanc_svg$startmonth, us_sanc_svg$startyear, us_sanc_svg$sanctions))
colnames(us_sanc_svg_df) <-c("month","year","one","svg")
us_sanc_svg_df$one <- NULL
nsd <- merge(nsd,us_sanc_svg_df, by=c("month","year"),all = TRUE)
nsd$svg[is.na(nsd$svg)] <- 0

### Antigua (58)
us_sanc_antigua <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==58)
us_sanc_antigua_df <- as.data.frame(table(us_sanc_antigua$startmonth, us_sanc_antigua$startyear, us_sanc_antigua$sanctions))
colnames(us_sanc_antigua_df) <-c("month","year","one","antigua")
us_sanc_antigua_df$one <- NULL
nsd <- merge(nsd,us_sanc_antigua_df, by=c("month","year"),all = TRUE)
nsd$antigua[is.na(nsd$antigua)] <- 0

### Mexico (70)  
us_sanc_mexico <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==70)
us_sanc_mexico_df <- as.data.frame(table(us_sanc_mexico$startmonth, us_sanc_mexico$startyear, us_sanc_mexico$sanctions))
colnames(us_sanc_mexico_df) <-c("month","year","one","mexico")
us_sanc_mexico_df$one <- NULL
nsd <- merge(nsd,us_sanc_mexico_df, by=c("month","year"),all = TRUE)
nsd$mexico[is.na(nsd$mexico)] <- 0

### Guatemala (90)
us_sanc_guatemala <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==90)
us_sanc_guatemala_df <- as.data.frame(table(us_sanc_guatemala$startmonth, us_sanc_guatemala$startyear, us_sanc_guatemala$sanctions))
colnames(us_sanc_guatemala_df) <-c("month","year","one","guatemala")
us_sanc_guatemala_df$one <- NULL
nsd <- merge(nsd,us_sanc_guatemala_df, by=c("month","year"),all = TRUE)
nsd$guatemala[is.na(nsd$guatemala)] <- 0

### Honduras (91)
us_sanc_honduras <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==91)
us_sanc_honduras_df <- as.data.frame(table(us_sanc_honduras$startmonth, us_sanc_honduras$startyear, us_sanc_honduras$sanctions))
colnames(us_sanc_honduras_df) <-c("month","year","one","honduras")
us_sanc_honduras_df$one <- NULL
nsd <- merge(nsd,us_sanc_honduras_df, by=c("month","year"),all = TRUE)
nsd$honduras[is.na(nsd$honduras)] <- 0

### El Salvador (92)   
us_sanc_elsalvador <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==92)
us_sanc_elsalvador_df <- as.data.frame(table(us_sanc_elsalvador$startmonth, us_sanc_elsalvador$startyear, us_sanc_elsalvador$sanctions))
colnames(us_sanc_elsalvador_df) <-c("month","year","one","elsalvador")
us_sanc_elsalvador_df$one <- NULL
nsd <- merge(nsd,us_sanc_elsalvador_df, by=c("month","year"),all = TRUE)
nsd$elsalvador[is.na(nsd$elsalvador)] <- 0

### Nicaragua (93) 
us_sanc_nicaragua <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==93)
us_sanc_nicaragua_df <- as.data.frame(table(us_sanc_nicaragua$startmonth, us_sanc_nicaragua$startyear, us_sanc_nicaragua$sanctions))
colnames(us_sanc_nicaragua_df) <-c("month","year","one","nicaragua")
us_sanc_nicaragua_df$one <- NULL
nsd <- merge(nsd,us_sanc_nicaragua_df, by=c("month","year"),all = TRUE)
nsd$nicaragua[is.na(nsd$nicaragua)] <- 0

### Costa Rica (94) 
us_sanc_costarica <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==94)
us_sanc_costarica_df <- as.data.frame(table(us_sanc_costarica$startmonth, us_sanc_costarica$startyear, us_sanc_costarica$sanctions))
colnames(us_sanc_costarica_df) <-c("month","year","one","costarica")
us_sanc_costarica_df$one <- NULL
nsd <- merge(nsd,us_sanc_costarica_df, by=c("month","year"),all = TRUE)
nsd$costarica[is.na(nsd$costarica)] <- 0

### Panama (95)  
us_sanc_panama <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==95)
us_sanc_panama_df <- as.data.frame(table(us_sanc_panama$startmonth, us_sanc_panama$startyear, us_sanc_panama$sanctions))
colnames(us_sanc_panama_df) <-c("month","year","one","panama")
us_sanc_panama_df$one <- NULL
nsd <- merge(nsd,us_sanc_panama_df, by=c("month","year"),all = TRUE)
nsd$panama[is.na(nsd$panama)] <- 0

### Colombia (100) 
us_sanc_colombia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==100)
us_sanc_colombia_df <- as.data.frame(table(us_sanc_colombia$startmonth, us_sanc_colombia$startyear, us_sanc_colombia$sanctions))
colnames(us_sanc_colombia_df) <-c("month","year","one","colombia")
us_sanc_colombia_df$one <- NULL
nsd <- merge(nsd,us_sanc_colombia_df, by=c("month","year"),all = TRUE)
nsd$colombia[is.na(nsd$colombia)] <- 0

### Venezuela (101)  
us_sanc_venezuela <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==101)
us_sanc_venezuela_df <- as.data.frame(table(us_sanc_venezuela$startmonth, us_sanc_venezuela$startyear, us_sanc_venezuela$sanctions))
colnames(us_sanc_venezuela_df) <-c("month","year","one","venezuela")
us_sanc_venezuela_df$one <- NULL
nsd <- merge(nsd,us_sanc_venezuela_df, by=c("month","year"),all = TRUE)
nsd$venezuela[is.na(nsd$venezuela)] <- 0

### Ecudador (130)  
us_sanc_ecuador <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==130)
us_sanc_ecuador_df <- as.data.frame(table(us_sanc_ecuador$startmonth, us_sanc_ecuador$startyear, us_sanc_ecuador$sanctions))
colnames(us_sanc_ecuador_df) <-c("month","year","one","ecuador")
us_sanc_ecuador_df$one <- NULL
nsd <- merge(nsd,us_sanc_ecuador_df, by=c("month","year"),all = TRUE)
nsd$ecuador[is.na(nsd$ecuador)] <- 0

### Peru (135)  
us_sanc_peru <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==135)
us_sanc_peru_df <- as.data.frame(table(us_sanc_peru$startmonth, us_sanc_peru$startyear, us_sanc_peru$sanctions))
colnames(us_sanc_peru_df) <-c("month","year","one","peru")
us_sanc_peru_df$one <- NULL
nsd <- merge(nsd,us_sanc_peru_df, by=c("month","year"),all = TRUE)
nsd$peru[is.na(nsd$peru)] <- 0

### Brazil (140)  
us_sanc_brazil <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==140)
us_sanc_brazil_df <- as.data.frame(table(us_sanc_brazil$startmonth, us_sanc_brazil$startyear, us_sanc_brazil$sanctions))
colnames(us_sanc_brazil_df) <-c("month","year","one","brazil")
us_sanc_brazil_df$one <- NULL
nsd <- merge(nsd,us_sanc_brazil_df, by=c("month","year"),all = TRUE)
nsd$brazil[is.na(nsd$brazil)] <- 0

### Bolivia (145) 
us_sanc_bolivia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==145)
us_sanc_bolivia_df <- as.data.frame(table(us_sanc_bolivia$startmonth, us_sanc_bolivia$startyear, us_sanc_bolivia$sanctions))
colnames(us_sanc_bolivia_df) <-c("month","year","one","bolivia")
us_sanc_bolivia_df$one <- NULL
nsd <- merge(nsd,us_sanc_bolivia_df, by=c("month","year"),all = TRUE)
nsd$bolivia[is.na(nsd$bolivia)] <- 0

### Paraguay (150)  
us_sanc_paraguay <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==150)
us_sanc_paraguay_df <- as.data.frame(table(us_sanc_paraguay$startmonth, us_sanc_paraguay$startyear, us_sanc_paraguay$sanctions))
colnames(us_sanc_paraguay_df) <-c("month","year","one","paraguay")
us_sanc_paraguay_df$one <- NULL
nsd <- merge(nsd,us_sanc_paraguay_df, by=c("month","year"),all = TRUE)
nsd$paraguay[is.na(nsd$paraguay)] <- 0

### Chile (155)  
us_sanc_chile <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==155)
us_sanc_chile_df <- as.data.frame(table(us_sanc_chile$startmonth, us_sanc_chile$startyear, us_sanc_chile$sanctions))
colnames(us_sanc_chile_df) <-c("month","year","one","chile")
us_sanc_chile_df$one <- NULL
nsd <- merge(nsd,us_sanc_chile_df, by=c("month","year"),all = TRUE)
nsd$chile[is.na(nsd$chile)] <- 0

### Argentina (160) 
us_sanc_argentina <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==160)
us_sanc_argentina_df <- as.data.frame(table(us_sanc_argentina$startmonth, us_sanc_argentina$startyear, us_sanc_argentina$sanctions))
colnames(us_sanc_argentina_df) <-c("month","year","one","argentina")
us_sanc_argentina_df$one <- NULL
nsd <- merge(nsd,us_sanc_argentina_df, by=c("month","year"),all = TRUE)
nsd$argentina[is.na(nsd$argentina)] <- 0

### Uruguay (165)  
us_sanc_uruguay <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==165)
us_sanc_uruguay_df <- as.data.frame(table(us_sanc_uruguay$startmonth, us_sanc_uruguay$startyear, us_sanc_uruguay$sanctions))
colnames(us_sanc_uruguay_df) <-c("month","year","one","uruguay")
us_sanc_uruguay_df$one <- NULL
nsd <- merge(nsd,us_sanc_uruguay_df, by=c("month","year"),all = TRUE)
nsd$uruguay[is.na(nsd$uruguay)] <- 0

### UK (200)  
us_sanc_uk <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==200)
us_sanc_uk_df <- as.data.frame(table(us_sanc_uk$startmonth, us_sanc_uk$startyear, us_sanc_uk$sanctions))
colnames(us_sanc_uk_df) <-c("month","year","one","uk")
us_sanc_uk_df$one <- NULL
nsd <- merge(nsd,us_sanc_uk_df, by=c("month","year"),all = TRUE)
nsd$uk[is.na(nsd$uk)] <- 0

### Ireland (205)  
us_sanc_ireland <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==205)
us_sanc_ireland_df <- as.data.frame(table(us_sanc_ireland$startmonth, us_sanc_ireland$startyear, us_sanc_ireland$sanctions))
colnames(us_sanc_ireland_df) <-c("month","year","one","ireland")
us_sanc_ireland_df$one <- NULL
nsd <- merge(nsd,us_sanc_ireland_df, by=c("month","year"),all = TRUE)
nsd$ireland[is.na(nsd$ireland)] <- 0

### Netherlands (210)  
us_sanc_netherlands <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==210)
us_sanc_netherlands_df <- as.data.frame(table(us_sanc_netherlands$startmonth, us_sanc_netherlands$startyear, us_sanc_netherlands$sanctions))
colnames(us_sanc_netherlands_df) <-c("month","year","one","netherlands")
us_sanc_netherlands_df$one <- NULL
nsd <- merge(nsd,us_sanc_netherlands_df, by=c("month","year"),all = TRUE)
nsd$netherlands[is.na(nsd$netherlands)] <- 0

### Belgium (211)  
us_sanc_belgium <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==211)
us_sanc_belgium_df <- as.data.frame(table(us_sanc_belgium$startmonth, us_sanc_belgium$startyear, us_sanc_belgium$sanctions))
colnames(us_sanc_belgium_df) <-c("month","year","one","belgium")
us_sanc_belgium_df$one <- NULL
nsd <- merge(nsd,us_sanc_belgium_df, by=c("month","year"),all = TRUE)
nsd$belgium[is.na(nsd$belgium)] <- 0

### Luxembourg (212)  
us_sanc_luxembourg <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==212)
us_sanc_luxembourg_df <- as.data.frame(table(us_sanc_luxembourg$startmonth, us_sanc_luxembourg$startyear, us_sanc_luxembourg$sanctions))
colnames(us_sanc_luxembourg_df) <-c("month","year","one","luxembourg")
us_sanc_luxembourg_df$one <- NULL
nsd <- merge(nsd,us_sanc_luxembourg_df, by=c("month","year"),all = TRUE)
nsd$luxembourg[is.na(nsd$luxembourg)] <- 0

### France (220)  
us_sanc_france <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==220)
us_sanc_france_df <- as.data.frame(table(us_sanc_france$startmonth, us_sanc_france$startyear, us_sanc_france$sanctions))
colnames(us_sanc_france_df) <-c("month","year","one","france")
us_sanc_france_df$one <- NULL
nsd <- merge(nsd,us_sanc_france_df, by=c("month","year"),all = TRUE)
nsd$france[is.na(nsd$france)] <- 0

### Switzerland (225)  
us_sanc_switzerland <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==225)
us_sanc_switzerland_df <- as.data.frame(table(us_sanc_switzerland$startmonth, us_sanc_switzerland$startyear, us_sanc_switzerland$sanctions))
colnames(us_sanc_switzerland_df) <-c("month","year","one","switzerland")
us_sanc_switzerland_df$one <- NULL
nsd <- merge(nsd,us_sanc_switzerland_df, by=c("month","year"),all = TRUE)
nsd$switzerland[is.na(nsd$switzerland)] <- 0

### Spain (230)  
us_sanc_spain <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==230)
us_sanc_spain_df <- as.data.frame(table(us_sanc_spain$startmonth, us_sanc_spain$startyear, us_sanc_spain$sanctions))
colnames(us_sanc_spain_df) <-c("month","year","one","spain")
us_sanc_spain_df$one <- NULL
nsd <- merge(nsd,us_sanc_spain_df, by=c("month","year"),all = TRUE)
nsd$spain[is.na(nsd$spain)] <- 0

### Germany (255, 260)  
us_sanc_germany <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==255 | targetstate==260)
us_sanc_germany_df <- as.data.frame(table(us_sanc_germany$startmonth, us_sanc_germany$startyear, us_sanc_germany$sanctions))
colnames(us_sanc_germany_df) <-c("month","year","one","germany")
us_sanc_germany_df$one <- NULL
nsd <- merge(nsd,us_sanc_germany_df, by=c("month","year"),all = TRUE)
nsd$germany[is.na(nsd$germany)] <- 0

### East Germany (265)  
us_sanc_eastgermany <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==265)
us_sanc_eastgermany_df <- as.data.frame(table(us_sanc_eastgermany$startmonth, us_sanc_eastgermany$startyear, us_sanc_eastgermany$sanctions))
colnames(us_sanc_eastgermany_df) <-c("month","year","one","eastgermany")
us_sanc_eastgermany_df$one <- NULL
nsd <- merge(nsd,us_sanc_eastgermany_df, by=c("month","year"),all = TRUE)
nsd$eastgermany[is.na(nsd$eastgermany)] <- 0

### Poland (290)  
us_sanc_poland <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==290)
us_sanc_poland_df <- as.data.frame(table(us_sanc_poland$startmonth, us_sanc_poland$startyear, us_sanc_poland$sanctions))
colnames(us_sanc_poland_df) <-c("month","year","one","poland")
us_sanc_poland_df$one <- NULL
nsd <- merge(nsd,us_sanc_poland_df, by=c("month","year"),all = TRUE)
nsd$poland[is.na(nsd$poland)] <- 0

### Austria (305)  
us_sanc_austria <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==305)
us_sanc_austria_df <- as.data.frame(table(us_sanc_austria$startmonth, us_sanc_austria$startyear, us_sanc_austria$sanctions))
colnames(us_sanc_austria_df) <-c("month","year","one","austria")
us_sanc_austria_df$one <- NULL
nsd <- merge(nsd,us_sanc_austria_df, by=c("month","year"),all = TRUE)
nsd$austria[is.na(nsd$austria)] <- 0

### Hungary (310) 
us_sanc_hungary <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==310)
us_sanc_hungary_df <- as.data.frame(table(us_sanc_hungary$startmonth, us_sanc_hungary$startyear, us_sanc_hungary$sanctions))
colnames(us_sanc_hungary_df) <-c("month","year","one","hungary")
us_sanc_hungary_df$one <- NULL
nsd <- merge(nsd,us_sanc_hungary_df, by=c("month","year"),all = TRUE)
nsd$hungary[is.na(nsd$hungary)] <- 0

### Czechoslovakia (315)  
us_sanc_czechoslovakia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==315)
us_sanc_czechoslovakia_df <- as.data.frame(table(us_sanc_czechoslovakia$startmonth, us_sanc_czechoslovakia$startyear, us_sanc_czechoslovakia$sanctions))
colnames(us_sanc_czechoslovakia_df) <-c("month","year","one","czechoslovakia")
us_sanc_czechoslovakia_df$one <- NULL
nsd <- merge(nsd,us_sanc_czechoslovakia_df, by=c("month","year"),all = TRUE)
nsd$czechoslovakia[is.na(nsd$czechoslovakia)] <- 0

### Slovakia (317)  
us_sanc_slovakia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==317)
us_sanc_slovakia_df <- as.data.frame(table(us_sanc_slovakia$startmonth, us_sanc_slovakia$startyear, us_sanc_slovakia$sanctions))
colnames(us_sanc_slovakia_df) <-c("month","year","one","slovakia")
us_sanc_slovakia_df$one <- NULL
nsd <- merge(nsd,us_sanc_slovakia_df, by=c("month","year"),all = TRUE)
nsd$slovakia[is.na(nsd$slovakia)] <- 0

### Italy (325)  
us_sanc_italy <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==325)
us_sanc_italy_df <- as.data.frame(table(us_sanc_italy$startmonth, us_sanc_italy$startyear, us_sanc_italy$sanctions))
colnames(us_sanc_italy_df) <-c("month","year","one","italy")
us_sanc_italy_df$one <- NULL
nsd <- merge(nsd,us_sanc_italy_df, by=c("month","year"),all = TRUE)
nsd$italy[is.na(nsd$italy)] <- 0

### Malta (338) 
us_sanc_malta <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==338)
us_sanc_malta_df <- as.data.frame(table(us_sanc_malta$startmonth, us_sanc_malta$startyear, us_sanc_malta$sanctions))
colnames(us_sanc_malta_df) <-c("month","year","one","malta")
us_sanc_malta_df$one <- NULL
nsd <- merge(nsd,us_sanc_malta_df, by=c("month","year"),all = TRUE)
nsd$malta[is.na(nsd$malta)] <- 0

### Albania (339)  
us_sanc_albania <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==339)
us_sanc_albania_df <- as.data.frame(table(us_sanc_albania$startmonth, us_sanc_albania$startyear, us_sanc_albania$sanctions))
colnames(us_sanc_albania_df) <-c("month","year","one","albania")
us_sanc_albania_df$one <- NULL
nsd <- merge(nsd,us_sanc_albania_df, by=c("month","year"),all = TRUE)
nsd$albania[is.na(nsd$albania)] <- 0

### Croatia (344)  
us_sanc_croatia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==344)
us_sanc_croatia_df <- as.data.frame(table(us_sanc_croatia$startmonth, us_sanc_croatia$startyear, us_sanc_croatia$sanctions))
colnames(us_sanc_croatia_df) <-c("month","year","one","croatia")
us_sanc_croatia_df$one <- NULL
nsd <- merge(nsd,us_sanc_croatia_df, by=c("month","year"),all = TRUE)
nsd$croatia[is.na(nsd$croatia)] <- 0

### Yugoslavia (345) 
us_sanc_yugoslavia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==345)
us_sanc_yugoslavia_df <- as.data.frame(table(us_sanc_yugoslavia$startmonth, us_sanc_yugoslavia$startyear, us_sanc_yugoslavia$sanctions))
colnames(us_sanc_yugoslavia_df) <-c("month","year","one","yugoslavia")
us_sanc_yugoslavia_df$one <- NULL
nsd <- merge(nsd,us_sanc_yugoslavia_df, by=c("month","year"),all = TRUE)
nsd$yugoslavia[is.na(nsd$yugoslavia)] <- 0

### Slovenia (349) 
us_sanc_slovenia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==349)
us_sanc_slovenia_df <- as.data.frame(table(us_sanc_slovenia$startmonth, us_sanc_slovenia$startyear, us_sanc_slovenia$sanctions))
colnames(us_sanc_slovenia_df) <-c("month","year","one","slovenia")
us_sanc_slovenia_df$one <- NULL
nsd <- merge(nsd,us_sanc_slovenia_df, by=c("month","year"),all = TRUE)
nsd$slovenia[is.na(nsd$slovenia)] <- 0

### Greece (350)  
us_sanc_greece <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==350)
us_sanc_greece_df <- as.data.frame(table(us_sanc_greece$startmonth, us_sanc_greece$startyear, us_sanc_greece$sanctions))
colnames(us_sanc_greece_df) <-c("month","year","one","greece")
us_sanc_greece_df$one <- NULL
nsd <- merge(nsd,us_sanc_greece_df, by=c("month","year"),all = TRUE)
nsd$greece[is.na(nsd$greece)] <- 0

### Cyprus (352) 
us_sanc_cyprus <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==352)
us_sanc_cyprus_df <- as.data.frame(table(us_sanc_cyprus$startmonth, us_sanc_cyprus$startyear, us_sanc_cyprus$sanctions))
colnames(us_sanc_cyprus_df) <-c("month","year","one","cyprus")
us_sanc_cyprus_df$one <- NULL
nsd <- merge(nsd,us_sanc_cyprus_df, by=c("month","year"),all = TRUE)
nsd$cyprus[is.na(nsd$cyprus)] <- 0

### Bulgaria (355) 
us_sanc_bulgaria <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==355)
us_sanc_bulgaria_df <- as.data.frame(table(us_sanc_bulgaria$startmonth, us_sanc_bulgaria$startyear, us_sanc_bulgaria$sanctions))
colnames(us_sanc_bulgaria_df) <-c("month","year","one","bulgaria")
us_sanc_bulgaria_df$one <- NULL
nsd <- merge(nsd,us_sanc_bulgaria_df, by=c("month","year"),all = TRUE)
nsd$bulgaria[is.na(nsd$bulgaria)] <- 0

### Romania (360) 
us_sanc_romania <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==360)
us_sanc_romania_df <- as.data.frame(table(us_sanc_romania$startmonth, us_sanc_romania$startyear, us_sanc_romania$sanctions))
colnames(us_sanc_romania_df) <-c("month","year","one","romania")
us_sanc_romania_df$one <- NULL
nsd <- merge(nsd,us_sanc_romania_df, by=c("month","year"),all = TRUE)
nsd$romania[is.na(nsd$romania)] <- 0

### Russia (365) 
us_sanc_russia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==365)
us_sanc_russia_df <- as.data.frame(table(us_sanc_russia$startmonth, us_sanc_russia$startyear, us_sanc_russia$sanctions))
colnames(us_sanc_russia_df) <-c("month","year","one","russia")
us_sanc_russia_df$one <- NULL
nsd <- merge(nsd,us_sanc_russia_df, by=c("month","year"),all = TRUE)
nsd$russia[is.na(nsd$russia)] <- 0

### Estonia (366) 
us_sanc_estonia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==366)
us_sanc_estonia_df <- as.data.frame(table(us_sanc_estonia$startmonth, us_sanc_estonia$startyear, us_sanc_estonia$sanctions))
colnames(us_sanc_estonia_df) <-c("month","year","one","estonia")
us_sanc_estonia_df$one <- NULL
nsd <- merge(nsd,us_sanc_estonia_df, by=c("month","year"),all = TRUE)
nsd$estonia[is.na(nsd$estonia)] <- 0

### Latvia (367) 
us_sanc_latvia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==367)
us_sanc_latvia_df <- as.data.frame(table(us_sanc_latvia$startmonth, us_sanc_latvia$startyear, us_sanc_latvia$sanctions))
colnames(us_sanc_latvia_df) <-c("month","year","one","latvia")
us_sanc_latvia_df$one <- NULL
nsd <- merge(nsd,us_sanc_latvia_df, by=c("month","year"),all = TRUE)
nsd$latvia[is.na(nsd$latvia)] <- 0

### Lithuania (368) 
us_sanc_lithuania <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==368)
us_sanc_lithuania_df <- as.data.frame(table(us_sanc_lithuania$startmonth, us_sanc_lithuania$startyear, us_sanc_lithuania$sanctions))
colnames(us_sanc_lithuania_df) <-c("month","year","one","lithuania")
us_sanc_lithuania_df$one <- NULL
nsd <- merge(nsd,us_sanc_lithuania_df, by=c("month","year"),all = TRUE)
nsd$lithuania[is.na(nsd$lithuania)] <- 0

### Ukraine (369) 
us_sanc_ukraine <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==369)
us_sanc_ukraine_df <- as.data.frame(table(us_sanc_ukraine$startmonth, us_sanc_ukraine$startyear, us_sanc_ukraine$sanctions))
colnames(us_sanc_ukraine_df) <-c("month","year","one","ukraine")
us_sanc_ukraine_df$one <- NULL
nsd <- merge(nsd,us_sanc_ukraine_df, by=c("month","year"),all = TRUE)
nsd$ukraine[is.na(nsd$ukraine)] <- 0

### Belarus (370) 
us_sanc_belarus <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==370)
us_sanc_belarus_df <- as.data.frame(table(us_sanc_belarus$startmonth, us_sanc_belarus$startyear, us_sanc_belarus$sanctions))
colnames(us_sanc_belarus_df) <-c("month","year","one","belarus")
us_sanc_belarus_df$one <- NULL
nsd <- merge(nsd,us_sanc_belarus_df, by=c("month","year"),all = TRUE)
nsd$belarus[is.na(nsd$belarus)] <- 0

### Azerbaijan (373) 
us_sanc_azerbaijan <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==373)
us_sanc_azerbaijan_df <- as.data.frame(table(us_sanc_azerbaijan$startmonth, us_sanc_azerbaijan$startyear, us_sanc_azerbaijan$sanctions))
colnames(us_sanc_azerbaijan_df) <-c("month","year","one","azerbaijan")
us_sanc_azerbaijan_df$one <- NULL
nsd <- merge(nsd,us_sanc_azerbaijan_df, by=c("month","year"),all = TRUE)
nsd$azerbaijan[is.na(nsd$azerbaijan)] <- 0

### Finland (375) 
us_sanc_finland <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==375)
us_sanc_finland_df <- as.data.frame(table(us_sanc_finland$startmonth, us_sanc_finland$startyear, us_sanc_finland$sanctions))
colnames(us_sanc_finland_df) <-c("month","year","one","finland")
us_sanc_finland_df$one <- NULL
nsd <- merge(nsd,us_sanc_finland_df, by=c("month","year"),all = TRUE)
nsd$finland[is.na(nsd$finland)] <- 0

### Sweden (380) 
us_sanc_sweden <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==380)
us_sanc_sweden_df <- as.data.frame(table(us_sanc_sweden$startmonth, us_sanc_sweden$startyear, us_sanc_sweden$sanctions))
colnames(us_sanc_sweden_df) <-c("month","year","one","sweden")
us_sanc_sweden_df$one <- NULL
nsd <- merge(nsd,us_sanc_sweden_df, by=c("month","year"),all = TRUE)
nsd$sweden[is.na(nsd$sweden)] <- 0

### Norway (385)
us_sanc_norway <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==385)
us_sanc_norway_df <- as.data.frame(table(us_sanc_norway$startmonth, us_sanc_norway$startyear, us_sanc_norway$sanctions))
colnames(us_sanc_norway_df) <-c("month","year","one","norway")
us_sanc_norway_df$one <- NULL
nsd <- merge(nsd,us_sanc_norway_df, by=c("month","year"),all = TRUE)
nsd$norway[is.na(nsd$norway)] <- 0

### Mali (432)
us_sanc_mali <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==432)
us_sanc_mali_df <- as.data.frame(table(us_sanc_mali$startmonth, us_sanc_mali$startyear, us_sanc_mali$sanctions))
colnames(us_sanc_mali_df) <-c("month","year","one","mali")
us_sanc_mali_df$one <- NULL
nsd <- merge(nsd,us_sanc_mali_df, by=c("month","year"),all = TRUE)
nsd$mali[is.na(nsd$mali)] <- 0

### Benin (434) 
us_sanc_benin <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==434)
us_sanc_benin_df <- as.data.frame(table(us_sanc_benin$startmonth, us_sanc_benin$startyear, us_sanc_benin$sanctions))
colnames(us_sanc_benin_df) <-c("month","year","one","benin")
us_sanc_benin_df$one <- NULL
nsd <- merge(nsd,us_sanc_benin_df, by=c("month","year"),all = TRUE)
nsd$benin[is.na(nsd$benin)] <- 0

### Niger (436) 
us_sanc_niger <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==436)
us_sanc_niger_df <- as.data.frame(table(us_sanc_niger$startmonth, us_sanc_niger$startyear, us_sanc_niger$sanctions))
colnames(us_sanc_niger_df) <-c("month","year","one","niger")
us_sanc_niger_df$one <- NULL
nsd <- merge(nsd,us_sanc_niger_df, by=c("month","year"),all = TRUE)
nsd$niger[is.na(nsd$niger)] <- 0

### Ivory Coast (437) 
us_sanc_ivorycoast <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==437)
us_sanc_ivorycoast_df <- as.data.frame(table(us_sanc_ivorycoast$startmonth, us_sanc_ivorycoast$startyear, us_sanc_ivorycoast$sanctions))
colnames(us_sanc_ivorycoast_df) <-c("month","year","one","ivorycoast")
us_sanc_ivorycoast_df$one <- NULL
nsd <- merge(nsd,us_sanc_ivorycoast_df, by=c("month","year"),all = TRUE)
nsd$ivorycoast[is.na(nsd$ivorycoast)] <- 0

### Guinea (438) 
us_sanc_guinea <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==438)
us_sanc_guinea_df <- as.data.frame(table(us_sanc_guinea$startmonth, us_sanc_guinea$startyear, us_sanc_guinea$sanctions))
colnames(us_sanc_guinea_df) <-c("month","year","one","guinea")
us_sanc_guinea_df$one <- NULL
nsd <- merge(nsd,us_sanc_guinea_df, by=c("month","year"),all = TRUE)
nsd$guinea[is.na(nsd$guinea)] <- 0

### Liberia (450) 
us_sanc_liberia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==450)
us_sanc_liberia_df <- as.data.frame(table(us_sanc_liberia$startmonth, us_sanc_liberia$startyear, us_sanc_liberia$sanctions))
colnames(us_sanc_liberia_df) <-c("month","year","one","liberia")
us_sanc_liberia_df$one <- NULL
nsd <- merge(nsd,us_sanc_liberia_df, by=c("month","year"),all = TRUE)
nsd$liberia[is.na(nsd$liberia)] <- 0

### Nigeria (475) 
us_sanc_nigeria <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==475)
us_sanc_nigeria_df <- as.data.frame(table(us_sanc_nigeria$startmonth, us_sanc_nigeria$startyear, us_sanc_nigeria$sanctions))
colnames(us_sanc_nigeria_df) <-c("month","year","one","nigeria")
us_sanc_nigeria_df$one <- NULL
nsd <- merge(nsd,us_sanc_nigeria_df, by=c("month","year"),all = TRUE)
nsd$nigeria[is.na(nsd$nigeria)] <- 0

### Central African Republic (482)
us_sanc_CAR <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==482)
us_sanc_CAR_df <- as.data.frame(table(us_sanc_CAR$startmonth, us_sanc_CAR$startyear, us_sanc_CAR$sanctions))
colnames(us_sanc_CAR_df) <-c("month","year","one","CAR")
us_sanc_CAR_df$one <- NULL
nsd <- merge(nsd,us_sanc_CAR_df, by=c("month","year"),all = TRUE)
nsd$CAR[is.na(nsd$CAR)] <- 0

### Congo (484)
us_sanc_congo <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==484)
us_sanc_congo_df <- as.data.frame(table(us_sanc_congo$startmonth, us_sanc_congo$startyear, us_sanc_congo$sanctions))
colnames(us_sanc_congo_df) <-c("month","year","one","congo")
us_sanc_congo_df$one <- NULL
nsd <- merge(nsd,us_sanc_congo_df, by=c("month","year"),all = TRUE)
nsd$congo[is.na(nsd$congo)] <- 0

### Democratic Republic of Congo (490)
us_sanc_DRC <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==490)
us_sanc_DRC_df <- as.data.frame(table(us_sanc_DRC$startmonth, us_sanc_DRC$startyear, us_sanc_DRC$sanctions))
colnames(us_sanc_DRC_df) <-c("month","year","one","DRC")
us_sanc_DRC_df$one <- NULL
nsd <- merge(nsd,us_sanc_DRC_df, by=c("month","year"),all = TRUE)
nsd$DRC[is.na(nsd$DRC)] <- 0

### Kenya (501) 
us_sanc_kenya <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==501)
us_sanc_kenya_df <- as.data.frame(table(us_sanc_kenya$startmonth, us_sanc_kenya$startyear, us_sanc_kenya$sanctions))
colnames(us_sanc_kenya_df) <-c("month","year","one","kenya")
us_sanc_kenya_df$one <- NULL
nsd <- merge(nsd,us_sanc_kenya_df, by=c("month","year"),all = TRUE)
nsd$kenya[is.na(nsd$kenya)] <- 0

### Tanzania (510) 
us_sanc_tanzania <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==510)
us_sanc_tanzania_df <- as.data.frame(table(us_sanc_tanzania$startmonth, us_sanc_tanzania$startyear, us_sanc_tanzania$sanctions))
colnames(us_sanc_tanzania_df) <-c("month","year","one","tanzania")
us_sanc_tanzania_df$one <- NULL
nsd <- merge(nsd,us_sanc_tanzania_df, by=c("month","year"),all = TRUE)
nsd$tanzania[is.na(nsd$tanzania)] <- 0

### Ethiopia (530)
us_sanc_ethiopia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==530)
us_sanc_ethiopia_df <- as.data.frame(table(us_sanc_ethiopia$startmonth, us_sanc_ethiopia$startyear, us_sanc_ethiopia$sanctions))
colnames(us_sanc_ethiopia_df) <-c("month","year","one","ethiopia")
us_sanc_ethiopia_df$one <- NULL
nsd <- merge(nsd,us_sanc_ethiopia_df, by=c("month","year"),all = TRUE)
nsd$ethiopia[is.na(nsd$ethiopia)] <- 0

### Zambia (551) 
us_sanc_zambia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==551)
us_sanc_zambia_df <- as.data.frame(table(us_sanc_zambia$startmonth, us_sanc_zambia$startyear, us_sanc_zambia$sanctions))
colnames(us_sanc_zambia_df) <-c("month","year","one","zambia")
us_sanc_zambia_df$one <- NULL
nsd <- merge(nsd,us_sanc_zambia_df, by=c("month","year"),all = TRUE)
nsd$zambia[is.na(nsd$zambia)] <- 0

### Zimbabwe (552)
us_sanc_zimbabwe <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==552)
us_sanc_zimbabwe_df <- as.data.frame(table(us_sanc_zimbabwe$startmonth, us_sanc_zimbabwe$startyear, us_sanc_zimbabwe$sanctions))
colnames(us_sanc_zimbabwe_df) <-c("month","year","one","zimbabwe")
us_sanc_zimbabwe_df$one <- NULL
nsd <- merge(nsd,us_sanc_zimbabwe_df, by=c("month","year"),all = TRUE)
nsd$zimbabwe[is.na(nsd$zimbabwe)] <- 0

### Malawi (553)
us_sanc_malawi <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==553)
us_sanc_malawi_df <- as.data.frame(table(us_sanc_malawi$startmonth, us_sanc_malawi$startyear, us_sanc_malawi$sanctions))
colnames(us_sanc_malawi_df) <-c("month","year","one","malawi")
us_sanc_malawi_df$one <- NULL
nsd <- merge(nsd,us_sanc_malawi_df, by=c("month","year"),all = TRUE)
nsd$malawi[is.na(nsd$malawi)] <- 0

### South Africa (560) 
us_sanc_southafrica <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==560)
us_sanc_southafrica_df <- as.data.frame(table(us_sanc_southafrica$startmonth, us_sanc_southafrica$startyear, us_sanc_southafrica$sanctions))
colnames(us_sanc_southafrica_df) <-c("month","year","one","southafrica")
us_sanc_southafrica_df$one <- NULL
nsd <- merge(nsd,us_sanc_southafrica_df, by=c("month","year"),all = TRUE)
nsd$southafrica[is.na(nsd$southafrica)] <- 0

### Namibia (565) 
us_sanc_namibia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==565)
us_sanc_namibia_df <- as.data.frame(table(us_sanc_namibia$startmonth, us_sanc_namibia$startyear, us_sanc_namibia$sanctions))
colnames(us_sanc_namibia_df) <-c("month","year","one","namibia")
us_sanc_namibia_df$one <- NULL
nsd <- merge(nsd,us_sanc_namibia_df, by=c("month","year"),all = TRUE)
nsd$namibia[is.na(nsd$namibia)] <- 0

### Lesotho (570) 
us_sanc_lesotho <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==570)
us_sanc_lesotho_df <- as.data.frame(table(us_sanc_lesotho$startmonth, us_sanc_lesotho$startyear, us_sanc_lesotho$sanctions))
colnames(us_sanc_lesotho_df) <-c("month","year","one","lesotho")
us_sanc_lesotho_df$one <- NULL
nsd <- merge(nsd,us_sanc_lesotho_df, by=c("month","year"),all = TRUE)
nsd$lesotho[is.na(nsd$lesotho)] <- 0

### Morocco (600)
us_sanc_morocco <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==600)
us_sanc_morocco_df <- as.data.frame(table(us_sanc_morocco$startmonth, us_sanc_morocco$startyear, us_sanc_morocco$sanctions))
colnames(us_sanc_morocco_df) <-c("month","year","one","morocco")
us_sanc_morocco_df$one <- NULL
nsd <- merge(nsd,us_sanc_morocco_df, by=c("month","year"),all = TRUE)
nsd$morocco[is.na(nsd$morocco)] <- 0

### Algeria (615) 
us_sanc_algeria <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==615)
us_sanc_algeria_df <- as.data.frame(table(us_sanc_algeria$startmonth, us_sanc_algeria$startyear, us_sanc_algeria$sanctions))
colnames(us_sanc_algeria_df) <-c("month","year","one","algeria")
us_sanc_algeria_df$one <- NULL
nsd <- merge(nsd,us_sanc_algeria_df, by=c("month","year"),all = TRUE)
nsd$algeria[is.na(nsd$algeria)] <- 0

### Libya (620)
us_sanc_libya <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==620)
us_sanc_libya_df <- as.data.frame(table(us_sanc_libya$startmonth, us_sanc_libya$startyear, us_sanc_libya$sanctions))
colnames(us_sanc_libya_df) <-c("month","year","one","libya")
us_sanc_libya_df$one <- NULL
nsd <- merge(nsd,us_sanc_libya_df, by=c("month","year"),all = TRUE)
nsd$libya[is.na(nsd$libya)] <- 0

### Sudan (625) 
us_sanc_sudan <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==625)
us_sanc_sudan_df <- as.data.frame(table(us_sanc_sudan$startmonth, us_sanc_sudan$startyear, us_sanc_sudan$sanctions))
colnames(us_sanc_sudan_df) <-c("month","year","one","sudan")
us_sanc_sudan_df$one <- NULL
nsd <- merge(nsd,us_sanc_sudan_df, by=c("month","year"),all = TRUE)
nsd$sudan[is.na(nsd$sudan)] <- 0

### Iran (630) 
us_sanc_iran <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==630)
us_sanc_iran_df <- as.data.frame(table(us_sanc_iran$startmonth, us_sanc_iran$startyear, us_sanc_iran$sanctions))
colnames(us_sanc_iran_df) <-c("month","year","one","iran")
us_sanc_iran_df$one <- NULL
nsd <- merge(nsd,us_sanc_iran_df, by=c("month","year"),all = TRUE)
nsd$iran[is.na(nsd$iran)] <- 0

### Turkey (640)
us_sanc_turkey <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==640)
us_sanc_turkey_df <- as.data.frame(table(us_sanc_turkey$startmonth, us_sanc_turkey$startyear, us_sanc_turkey$sanctions))
colnames(us_sanc_turkey_df) <-c("month","year","one","turkey")
us_sanc_turkey_df$one <- NULL
nsd <- merge(nsd,us_sanc_turkey_df, by=c("month","year"),all = TRUE)
nsd$turkey[is.na(nsd$turkey)] <- 0

### Iraq (645) 
us_sanc_iraq <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==645)
us_sanc_iraq_df <- as.data.frame(table(us_sanc_iraq$startmonth, us_sanc_iraq$startyear, us_sanc_iraq$sanctions))
colnames(us_sanc_iraq_df) <-c("month","year","one","iraq")
us_sanc_iraq_df$one <- NULL
nsd <- merge(nsd,us_sanc_iraq_df, by=c("month","year"),all = TRUE)
nsd$iraq[is.na(nsd$iraq)] <- 0

### Egypt (651) 
us_sanc_egypt <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==651)
us_sanc_egypt_df <- as.data.frame(table(us_sanc_egypt$startmonth, us_sanc_egypt$startyear, us_sanc_egypt$sanctions))
colnames(us_sanc_egypt_df) <-c("month","year","one","egypt")
us_sanc_egypt_df$one <- NULL
nsd <- merge(nsd,us_sanc_egypt_df, by=c("month","year"),all = TRUE)
nsd$egypt[is.na(nsd$egypt)] <- 0

### Syria (652) 
us_sanc_syria <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==652)
us_sanc_syria_df <- as.data.frame(table(us_sanc_syria$startmonth, us_sanc_syria$startyear, us_sanc_syria$sanctions))
colnames(us_sanc_syria_df) <-c("month","year","one","syria")
us_sanc_syria_df$one <- NULL
nsd <- merge(nsd,us_sanc_syria_df, by=c("month","year"),all = TRUE)
nsd$syria[is.na(nsd$syria)] <- 0

### Lebanon (660) 
us_sanc_lebanon <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==660)
us_sanc_lebanon_df <- as.data.frame(table(us_sanc_lebanon$startmonth, us_sanc_lebanon$startyear, us_sanc_lebanon$sanctions))
colnames(us_sanc_lebanon_df) <-c("month","year","one","lebanon")
us_sanc_lebanon_df$one <- NULL
nsd <- merge(nsd,us_sanc_lebanon_df, by=c("month","year"),all = TRUE)
nsd$lebanon[is.na(nsd$lebanon)] <- 0

### Jordan (663)  
us_sanc_jordan <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==663)
us_sanc_jordan_df <- as.data.frame(table(us_sanc_jordan$startmonth, us_sanc_jordan$startyear, us_sanc_jordan$sanctions))
colnames(us_sanc_jordan_df) <-c("month","year","one","jordan")
us_sanc_jordan_df$one <- NULL
nsd <- merge(nsd,us_sanc_jordan_df, by=c("month","year"),all = TRUE)
nsd$jordan[is.na(nsd$jordan)] <- 0

### Israel (666)
us_sanc_israel <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==666)
us_sanc_israel_df <- as.data.frame(table(us_sanc_israel$startmonth, us_sanc_israel$startyear, us_sanc_israel$sanctions))
colnames(us_sanc_israel_df) <-c("month","year","one","israel")
us_sanc_israel_df$one <- NULL
nsd <- merge(nsd,us_sanc_israel_df, by=c("month","year"),all = TRUE)
nsd$israel[is.na(nsd$israel)] <- 0

### Saudi Arabia (670)
us_sanc_saudiarabia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==670)
us_sanc_saudiarabia_df <- as.data.frame(table(us_sanc_saudiarabia$startmonth, us_sanc_saudiarabia$startyear, us_sanc_saudiarabia$sanctions))
colnames(us_sanc_saudiarabia_df) <-c("month","year","one","saudiarabia")
us_sanc_saudiarabia_df$one <- NULL
nsd <- merge(nsd,us_sanc_saudiarabia_df, by=c("month","year"),all = TRUE)
nsd$saudiarabia[is.na(nsd$saudiarabia)] <- 0

### Afghanistan (700) 
us_sanc_afghanistan <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==700)
us_sanc_afghanistan_df <- as.data.frame(table(us_sanc_afghanistan$startmonth, us_sanc_afghanistan$startyear, us_sanc_afghanistan$sanctions))
colnames(us_sanc_afghanistan_df) <-c("month","year","one","afghanistan")
us_sanc_afghanistan_df$one <- NULL
nsd <- merge(nsd,us_sanc_afghanistan_df, by=c("month","year"),all = TRUE)
nsd$afghanistan[is.na(nsd$afghanistan)] <- 0

### China (710)
us_sanc_china <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==710)
us_sanc_china_df <- as.data.frame(table(us_sanc_china$startmonth, us_sanc_china$startyear, us_sanc_china$sanctions))
colnames(us_sanc_china_df) <-c("month","year","one","china")
us_sanc_china_df$one <- NULL
nsd <- merge(nsd,us_sanc_china_df, by=c("month","year"),all = TRUE)
nsd$china[is.na(nsd$china)] <- 0

### Taiwan (713) 
us_sanc_Taiwan <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==713)
us_sanc_Taiwan_df <- as.data.frame(table(us_sanc_Taiwan$startmonth, us_sanc_Taiwan$startyear, us_sanc_Taiwan$sanctions))
colnames(us_sanc_Taiwan_df) <-c("month","year","one","Taiwan")
us_sanc_Taiwan_df$one <- NULL
nsd <- merge(nsd,us_sanc_Taiwan_df, by=c("month","year"),all = TRUE)
nsd$Taiwan[is.na(nsd$Taiwan)] <- 0

### North Korea (731)
us_sanc_northkorea <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==731)
us_sanc_northkorea_df <- as.data.frame(table(us_sanc_northkorea$startmonth, us_sanc_northkorea$startyear, us_sanc_northkorea$sanctions))
colnames(us_sanc_northkorea_df) <-c("month","year","one","northkorea")
us_sanc_northkorea_df$one <- NULL
nsd <- merge(nsd,us_sanc_northkorea_df, by=c("month","year"),all = TRUE)
nsd$northkorea[is.na(nsd$northkorea)] <- 0

### South Korea (732) 
us_sanc_southkorea <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==732)
us_sanc_southkorea_df <- as.data.frame(table(us_sanc_southkorea$startmonth, us_sanc_southkorea$startyear, us_sanc_southkorea$sanctions))
colnames(us_sanc_southkorea_df) <-c("month","year","one","southkorea")
us_sanc_southkorea_df$one <- NULL
nsd <- merge(nsd,us_sanc_southkorea_df, by=c("month","year"),all = TRUE)
nsd$southkorea[is.na(nsd$southkorea)] <- 0

### Japan (740) 
us_sanc_japan <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==740)
us_sanc_japan_df <- as.data.frame(table(us_sanc_japan$startmonth, us_sanc_japan$startyear, us_sanc_japan$sanctions))
colnames(us_sanc_japan_df) <-c("month","year","one","japan")
us_sanc_japan_df$one <- NULL
nsd <- merge(nsd,us_sanc_japan_df, by=c("month","year"),all = TRUE)
nsd$japan[is.na(nsd$japan)] <- 0

### India (750) 
us_sanc_india <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==750)
us_sanc_india_df <- as.data.frame(table(us_sanc_india$startmonth, us_sanc_india$startyear, us_sanc_india$sanctions))
colnames(us_sanc_india_df) <-c("month","year","one","india")
us_sanc_india_df$one <- NULL
nsd <- merge(nsd,us_sanc_india_df, by=c("month","year"),all = TRUE)
nsd$india[is.na(nsd$india)] <- 0

### Pakistan (770) 
us_sanc_pakistan <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==770)
us_sanc_pakistan_df <- as.data.frame(table(us_sanc_pakistan$startmonth, us_sanc_pakistan$startyear, us_sanc_pakistan$sanctions))
colnames(us_sanc_pakistan_df) <-c("month","year","one","pakistan")
us_sanc_pakistan_df$one <- NULL
nsd <- merge(nsd,us_sanc_pakistan_df, by=c("month","year"),all = TRUE)
nsd$pakistan[is.na(nsd$pakistan)] <- 0

### Myanmar (775)
us_sanc_myanmar <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==775)
us_sanc_myanmar_df <- as.data.frame(table(us_sanc_myanmar$startmonth, us_sanc_myanmar$startyear, us_sanc_myanmar$sanctions))
colnames(us_sanc_myanmar_df) <-c("month","year","one","myanmar")
us_sanc_myanmar_df$one <- NULL
nsd <- merge(nsd,us_sanc_myanmar_df, by=c("month","year"),all = TRUE)
nsd$myanmar[is.na(nsd$myanmar)] <- 0

### Sri Lanka (780) 
us_sanc_srilanka <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==780)
us_sanc_srilanka_df <- as.data.frame(table(us_sanc_srilanka$startmonth, us_sanc_srilanka$startyear, us_sanc_srilanka$sanctions))
colnames(us_sanc_srilanka_df) <-c("month","year","one","srilanka")
us_sanc_srilanka_df$one <- NULL
nsd <- merge(nsd,us_sanc_srilanka_df, by=c("month","year"),all = TRUE)
nsd$srilanka[is.na(nsd$srilanka)] <- 0

### Thailand (800) 
us_sanc_thailand <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==800)
us_sanc_thailand_df <- as.data.frame(table(us_sanc_thailand$startmonth, us_sanc_thailand$startyear, us_sanc_thailand$sanctions))
colnames(us_sanc_thailand_df) <-c("month","year","one","thailand")
us_sanc_thailand_df$one <- NULL
nsd <- merge(nsd,us_sanc_thailand_df, by=c("month","year"),all = TRUE)
nsd$thailand[is.na(nsd$thailand)] <- 0

### Cambodia (811)
us_sanc_cambodia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==811)
us_sanc_cambodia_df <- as.data.frame(table(us_sanc_cambodia$startmonth, us_sanc_cambodia$startyear, us_sanc_cambodia$sanctions))
colnames(us_sanc_cambodia_df) <-c("month","year","one","cambodia")
us_sanc_cambodia_df$one <- NULL
nsd <- merge(nsd,us_sanc_cambodia_df, by=c("month","year"),all = TRUE)
nsd$cambodia[is.na(nsd$cambodia)] <- 0

### Laos (812) 
us_sanc_laos <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==812)
us_sanc_laos_df <- as.data.frame(table(us_sanc_laos$startmonth, us_sanc_laos$startyear, us_sanc_laos$sanctions))
colnames(us_sanc_laos_df) <-c("month","year","one","laos")
us_sanc_laos_df$one <- NULL
nsd <- merge(nsd,us_sanc_laos_df, by=c("month","year"),all = TRUE)
nsd$laos[is.na(nsd$laos)] <- 0

### North Vietnam (816)
us_sanc_northvietnam <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==816)
us_sanc_northvietnam_df <- as.data.frame(table(us_sanc_northvietnam$startmonth, us_sanc_northvietnam$startyear, us_sanc_northvietnam$sanctions))
colnames(us_sanc_northvietnam_df) <-c("month","year","one","northvietnam")
us_sanc_northvietnam_df$one <- NULL
nsd <- merge(nsd,us_sanc_northvietnam_df, by=c("month","year"),all = TRUE)
nsd$northvietnam[is.na(nsd$northvietnam)] <- 0

### South Vietnam (817) 
us_sanc_southvietnam <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==817)
us_sanc_southvietnam_df <- as.data.frame(table(us_sanc_southvietnam$startmonth, us_sanc_southvietnam$startyear, us_sanc_southvietnam$sanctions))
colnames(us_sanc_southvietnam_df) <-c("month","year","one","southvietnam")
us_sanc_southvietnam_df$one <- NULL
nsd <- merge(nsd,us_sanc_southvietnam_df, by=c("month","year"),all = TRUE)
nsd$southvietnam[is.na(nsd$southvietnam)] <- 0

### Malaysia (820) 
us_sanc_malaysia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==820)
us_sanc_malaysia_df <- as.data.frame(table(us_sanc_malaysia$startmonth, us_sanc_malaysia$startyear, us_sanc_malaysia$sanctions))
colnames(us_sanc_malaysia_df) <-c("month","year","one","malaysia")
us_sanc_malaysia_df$one <- NULL
nsd <- merge(nsd,us_sanc_malaysia_df, by=c("month","year"),all = TRUE)
nsd$malaysia[is.na(nsd$malaysia)] <- 0

### Singapore (830) 
us_sanc_singapore <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==830)
us_sanc_singapore_df <- as.data.frame(table(us_sanc_singapore$startmonth, us_sanc_singapore$startyear, us_sanc_singapore$sanctions))
colnames(us_sanc_singapore_df) <-c("month","year","one","singapore")
us_sanc_singapore_df$one <- NULL
nsd <- merge(nsd,us_sanc_singapore_df, by=c("month","year"),all = TRUE)
nsd$singapore[is.na(nsd$singapore)] <- 0

### Indonesia (850)
us_sanc_indonesia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==850)
us_sanc_indonesia_df <- as.data.frame(table(us_sanc_indonesia$startmonth, us_sanc_indonesia$startyear, us_sanc_indonesia$sanctions))
colnames(us_sanc_indonesia_df) <-c("month","year","one","indonesia")
us_sanc_indonesia_df$one <- NULL
nsd <- merge(nsd,us_sanc_indonesia_df, by=c("month","year"),all = TRUE)
nsd$indonesia[is.na(nsd$indonesia)] <- 0

### Australia (900) 
us_sanc_australia <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==900)
us_sanc_australia_df <- as.data.frame(table(us_sanc_australia$startmonth, us_sanc_australia$startyear, us_sanc_australia$sanctions))
colnames(us_sanc_australia_df) <-c("month","year","one","australia")
us_sanc_australia_df$one <- NULL
nsd <- merge(nsd,us_sanc_australia_df, by=c("month","year"),all = TRUE)
nsd$australia[is.na(nsd$australia)] <- 0

### New Zealand (920) 
us_sanc_newzealand <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==920)
us_sanc_newzealand_df <- as.data.frame(table(us_sanc_newzealand$startmonth, us_sanc_newzealand$startyear, us_sanc_newzealand$sanctions))
colnames(us_sanc_newzealand_df) <-c("month","year","one","newzealand")
us_sanc_newzealand_df$one <- NULL
nsd <- merge(nsd,us_sanc_newzealand_df, by=c("month","year"),all = TRUE)
nsd$newzealand[is.na(nsd$newzealand)] <- 0

### EU (1000) 
us_sanc_EU <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==1000)
us_sanc_EU_df <- as.data.frame(table(us_sanc_EU$startmonth, us_sanc_EU$startyear, us_sanc_EU$sanctions))
colnames(us_sanc_EU_df) <-c("month","year","one","EU")
us_sanc_EU_df$one <- NULL
nsd <- merge(nsd,us_sanc_EU_df, by=c("month","year"),all = TRUE)
nsd$EU[is.na(nsd$EU)] <- 0

### Vanuatu (935)
us_sanc_vanuatu <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==935)
us_sanc_vanuatu_df <- as.data.frame(table(us_sanc_vanuatu$startmonth, us_sanc_vanuatu$startyear, us_sanc_vanuatu$sanctions))
colnames(us_sanc_vanuatu_df) <-c("month","year","one","vanuatu")
us_sanc_vanuatu_df$one <- NULL
nsd <- merge(nsd,us_sanc_vanuatu_df, by=c("month","year"),all = TRUE)
nsd$vanuatu[is.na(nsd$vanuatu)] <- 0

### Fiji (950) 
us_sanc_fiji <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==950)
us_sanc_fiji_df <- as.data.frame(table(us_sanc_fiji$startmonth, us_sanc_fiji$startyear, us_sanc_fiji$sanctions))
colnames(us_sanc_fiji_df) <-c("month","year","one","fiji")
us_sanc_fiji_df$one <- NULL
nsd <- merge(nsd,us_sanc_fiji_df, by=c("month","year"),all = TRUE)
nsd$fiji[is.na(nsd$fiji)] <- 0

### Samoa (990)
us_sanc_samoa <- subset(subset(subset(sdat,primarysender==2), imposition==1), targetstate==990)
us_sanc_samoa_df <- as.data.frame(table(us_sanc_samoa$startmonth, us_sanc_samoa$startyear, us_sanc_samoa$sanctions))
colnames(us_sanc_samoa_df) <-c("month","year","one","samoa")
us_sanc_samoa_df$one <- NULL
nsd <- merge(nsd,us_sanc_samoa_df, by=c("month","year"),all = TRUE)
nsd$samoa[is.na(nsd$samoa)] <- 0

## Economic Variables

### Quandl Information

library(Quandl)

# Insert Your Key Here
Quandl.api_key('')

### Inflation
inflation_ts <- Quandl("RATEINF/INFLATION_USA")
inflation_ts <- inflation_ts[order(inflation_ts$Date),]
inflation_ts$year <- as.numeric(format(inflation_ts$Date,'%Y'))
inflation_ts$month <- as.numeric(format(inflation_ts$Date,'%m'))
colnames(inflation_ts) <- c("date","inflation","year","month")

### Unemployment

#### 1978 - 2014
unem1_ts <- Quandl("FRBC/UNEMP_ST_US")
unem1_ts <- unem1_ts[order(unem1_ts$Date),]
unem1_ts$year <- as.numeric(format(unem1_ts$Date,'%Y'))
unem1_ts$month <- as.numeric(format(unem1_ts$Date,'%m'))
colnames(unem1_ts) <- c("date","unem_1","year","month")

#### 1948 - 2011
unem2_ts <- Quandl("SWA/5G")
unem2_ts <- unem2_ts[order(unem2_ts$Date),]
unem2_ts$year <- as.numeric(format(unem2_ts$Date,'%Y'))
unem2_ts$month <- as.numeric(format(unem2_ts$Date,'%m'))
colnames(unem2_ts) <- c("date","unem_2","year","month")

### Consumer Sentiment 1955 - 2016
umics_ts <- Quandl("UMICH/SOC1")
umics_ts <- umics_ts[order(umics_ts$Date),]
umics_ts_78 <- subset(umics_ts, umics_ts$Date > "1977-11-30")
umics_ts_78$year <- as.numeric(format(umics_ts_78$Date,'%Y'))
umics_ts_78$month <- as.numeric(format(umics_ts_78$Date,'%m'))
colnames(umics_ts_78) <- c("date","umics","year","month")

## Presidential Approval
approval <- readstata13::read.dta13("approval-3.dta")

## Generate Final Data Set

### Subset of Sanctions Data
sdat_78_05 <- subset(nsd, nsd$year > 1977 & nsd$year < 2006)
sdat_78_05 <- sdat_78_05[order(sdat_78_05$year,sdat_78_05$month),]

### Subset of Approval Data
app_78_05 <- subset(approval, year > 1977 & year < 2006)
app_78_05 <- app_78_05[order(app_78_05$year,app_78_05$month),]
dat_78_05 <- cbind(sdat_78_05,approval = app_78_05$approval)

### Subset of Unemployment Data

#### Unemployment 1
unem1_78_05 <- subset(unem1_ts, unem1_ts$year > 1977 & unem1_ts$year < 2006)
dat_78_05 <- cbind(dat_78_05,unem1 = unem1_78_05$unem_1)

#### Unemployment 2
unem2_78_05 <- subset(unem2_ts, unem2_ts$year > 1977 & unem2_ts$year < 2006)
dat_78_05 <- cbind(dat_78_05,unem2 = unem2_78_05$unem_2)

### Subset of Inflation Data
inflation_78_05 <- subset(inflation_ts, inflation_ts$year > 1977 & inflation_ts$year < 2006)
dat_78_05 <- cbind(dat_78_05,inflation = inflation_78_05$inflation)

### Subset of Consumer Sentiment Data
umics_78_05 <- subset(umics_ts_78, umics_ts_78$year > 1977 & umics_ts_78$year < 2006)
dat_78_05 <- cbind(dat_78_05, umics = umics_78_05$umics)

## Date
dat_78_05 <- cbind(dat_78_05, date = umics_78_05$date)

## Events that affect presidential approval

### Election Years
dat_78_05$elecyear <- ifelse(dat_78_05$date > "1980-01-01" & dat_78_05$date < "1981-01-01" | dat_78_05$date > "1984-01-01" & dat_78_05$date < "1985-01-01"| dat_78_05$date > "1988-01-01" & dat_78_05$date < "1989-01-01"| dat_78_05$date > "1992-01-01" & dat_78_05$date < "1993-01-01"| dat_78_05$date > "1996-01-01" & dat_78_05$date < "1997-01-01"| dat_78_05$date > "2000-01-01" & dat_78_05$date < "2001-01-01"| dat_78_05$date > "2004-01-01" & dat_78_05$date < "2005-01-01",1,0)

### Inaugurations 
dat_78_05$inauguration <- ifelse(dat_78_05$date > "1980-01-01" & dat_78_05$date < "1980-02-01" | dat_78_05$date > "1984-01-01" & dat_78_05$date < "1984-02-01"| dat_78_05$date > "1988-01-01" & dat_78_05$date < "1988-02-01"| dat_78_05$date > "1992-01-01" & dat_78_05$date < "1992-02-01"| dat_78_05$date > "1996-01-01" & dat_78_05$date < "1996-02-01"| dat_78_05$date > "2000-01-01" & dat_78_05$date < "2000-02-01"| dat_78_05$date > "2004-01-01" & dat_78_05$date < "2004-02-01",1,0)

### Presidents

#### Ronald Reagan 
dat_78_05$reagan <- ifelse(dat_78_05$date > "1981-01-20" & dat_78_05$date < "1989-01-20", 1,0)

#### George HW Bush
dat_78_05$bush1 <- ifelse(dat_78_05$date > "1989-01-20" & dat_78_05$date < "1993-01-20", 1,0)

#### Bill Clinton 
dat_78_05$clinton <- ifelse(dat_78_05$date > "1993-01-20" & dat_78_05$date < "2000-01-20", 1,0)

#### George W Bush
dat_78_05$bush2 <- ifelse(dat_78_05$date > "2000-01-20", 1,0)

### Iran Hostage Crisis
dat_78_05$iranhostage <- ifelse(dat_78_05$date > "1979-01-01" & dat_78_05$date < "1981-02-01", 1,0)

### Attempted Assasination of Ronald Reagan
dat_78_05$reaganshot <- ifelse(dat_78_05$date > "1981-04-01" & dat_78_05$date < "1981-05-01", 1,0)

### Grenada Invasion
dat_78_05$grenada <- ifelse(dat_78_05$date > "1983-11-01" & dat_78_05$date < "1983-12-01", 1,0)

### Bombing of Marine Barraks in Libiya
dat_78_05$libyabomb <- ifelse(dat_78_05$date > "1986-02-01" & dat_78_05$date < "1986-05-01", 1,0)

### Iran Contra Affair
dat_78_05$irancontra <- ifelse(dat_78_05$date > "1986-10-01" & dat_78_05$date < "1987-09-01", 1,0)

### Invasion of Panama
dat_78_05$panamainvade <- ifelse(dat_78_05$date > "1989-12-01" & dat_78_05$date < "1990-02-01", 1,0)

### GOP - Dem Budget Summit
dat_78_05$budgetsummit <- ifelse(dat_78_05$date > "1990-10-01" & dat_78_05$date < "1990-11-01", 1,0)

### Persian Gulf War
dat_78_05$persiangulf <- ifelse(dat_78_05$date > "1990-07-01" & dat_78_05$date < "1991-03-01", 1,0)

### Somalia Operation
dat_78_05$somalia <- ifelse(dat_78_05$date > "1992-12-01" & dat_78_05$date < "1994-04-01", 1,0)

### UNSCOM Air Raids for Violation of Iraq No Fly Zone
dat_78_05$unscom <- ifelse(dat_78_05$date > "1993-01-01" & dat_78_05$date < "1993-02-01", 1,0)

### Haiti Operation 
dat_78_05$haitiinvade <- ifelse(dat_78_05$date > "1994-09-01" & dat_78_05$date < "1994-10-01", 1,0)

### Lewinsky Scandal
dat_78_05$lewinsky <- ifelse(dat_78_05$date > "1998-01-01" & dat_78_05$date < "1999-03-01", 1,0)

### Kosovo Invasion
dat_78_05$kosovoinvade <- ifelse(dat_78_05$date > "1999-03-01" & dat_78_05$date < "1999-07-01", 1,0)

### Operation Iraqi Freedom
dat_78_05$iraqifreedom <- ifelse(dat_78_05$date > "2003-03-20", 1,0)

### China Spy Plane Incident
dat_78_05$chinaspyplane <- ifelse(dat_78_05$date > "2001-04-01" & dat_78_05$date < "2001-05-01", 1,0)

### Hurrican Katrina
dat_78_05$katrina <- ifelse(dat_78_05$date > "2005-08-01" & dat_78_05$date < "2005-10-01", 1,0)

### Afghanistan Invasion
dat_78_05$afghanwar <- ifelse(dat_78_05$date > "2001-10-01", 1,0)

### 9-11 Attacks
dat_78_05$sep11 <- ifelse(dat_78_05$date > "2001-09-01" & dat_78_05$date < "2001-10-01", 1,0)

### Iraq Invasion
dat_78_05$iraqinvade <- ifelse(dat_78_05$date > "1990-08-01" & dat_78_05$date < "1990-09-01" | dat_78_05$date > "2003-03-01" & dat_78_05$date < "2003-04-01" , 1,0)

## Detrend Inflation Data
tcount <- 1:336
t2 <- tcount*tcount
t3 <- tcount*tcount*tcount
dt_inflation_mod <- dynlm::dynlm(inflation ~ tcount + t2 + t3,  data = dat_78_05)
dt_inflation <- dt_inflation_mod$residuals
dat_78_05 <- cbind(dat_78_05, dt_inflation = dt_inflation)
head(dat_78_05)

## Write data as .csv file
write.csv(dat_78_05, file = "sanc_rep_data.csv", row.names = FALSE)
